﻿Partial Public Class LogExport
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Content.Text = "<script language=""javascript""> var __xml=""" & GetDateTime() & """;</script>"
        End If
    End Sub

    Private Function GetDateTime() As String
        Dim endDay As Date = DateAdd(DateInterval.Day, -1.0, Now)
        Dim beginDay As Date = DateAdd(DateInterval.Month, -1.0, endDay)
        Dim enableRecentTime As String = GetEnableRecentTime() '有效最近时间
        Dim enableLastestTime As String = GetEnableLatestTime(enableRecentTime)
        Dim xml As String

        xml = "<times>"
        xml += "<begintime>" + beginDay.ToString("yyyy-MM-dd HH:mm:ss") + "</begintime>"
        xml += "<endtime>" + endDay.ToString("yyyy-MM-dd HH:mm:ss") + "</endtime>"
        xml += "<enablerecenttime>" + enableRecentTime + "</enablerecenttime>"
        xml += "<enablelatesttime>" + enableLastestTime + "</enablelatesttime>"
        xml += "</times>"

        Return xml
    End Function

    '有效采集最早时间
    Private Function GetEnableLatestTime(ByVal enableRecentTime As String) As String
        Dim result As String = ""
        Dim sql As String

        If enableRecentTime = "" Then
            Return result
        End If

        Dim dtTemp As Date
        Dim dtEnableRecentSubtractYear As Date  '最近有效采集时间-1年
        Dim dtEnableLatestTime As Date          '最早有效采集时间

        If Date.TryParse(enableRecentTime, dtTemp) Then
            dtEnableRecentSubtractYear = DateAdd(DateInterval.Year, -1.0, dtTemp) '最近有效采集时间-1年

            sql = "Select top 1 RecordTime  from Lms_RunRecord where RecordTime is not null  Order by RecordTime ASC"
            result = Mysoft.Map.LMS.MyDBExtend.GetDataItemString(sql)
            If Date.TryParse(result, dtEnableLatestTime) Then
                If dtEnableRecentSubtractYear < dtEnableLatestTime Then
                    Return dtEnableLatestTime.ToString("yyyy-MM-dd HH:mm:ss")
                Else
                    Return dtEnableRecentSubtractYear.ToString("yyyy-MM-dd HH:mm:ss")
                End If
            End If
        End If

        Return result
    End Function

    ' 有效采集最近时间
    Private Function GetEnableRecentTime() As String
        Dim result As String = ""
        Dim sql As String
        sql = "Select top 1 RecordTime  from Lms_RunRecord Order by RecordTime desc"

        result = Mysoft.Map.LMS.MyDBExtend.GetDataItemString(sql)
        If result <> "" Then
            Dim dtTemp As Date
            If Date.TryParse(result, dtTemp) Then
                Return dtTemp.ToString("yyyy-MM-dd HH:mm:ss")
            End If
        End If

        Return result
    End Function

End Class